package com.summerbird.mallchat.common.user.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.summerbird.mallchat.common.common.domain.vo.req.PageBaseReq;
import com.summerbird.mallchat.common.user.domain.entity.UserApply;
import com.summerbird.mallchat.common.user.mapper.UserApplyMapper;
import com.summerbird.mallchat.common.user.service.IUserApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.summerbird.mallchat.common.user.domain.enums.ApplyReadStatusEnum.READ;
import static com.summerbird.mallchat.common.user.domain.enums.ApplyReadStatusEnum.UNREAD;
import static com.summerbird.mallchat.common.user.domain.enums.ApplyStatusEnum.AGREE;

/**
 * <p>
 * 用户申请表 服务实现类
 * </p>
 *
 * @author <a href="https://github.com/zongzibinbin">abin</a>
 * @since 2024-01-08
 */
@Service
public class UserApplyDao extends ServiceImpl<UserApplyMapper, UserApply> {

    public UserApply getFriendApproving(Long uid, Long friendUid) {
        return lambdaQuery()
                .eq(UserApply::getUid, uid)
                .eq(UserApply::getTargetId, friendUid)
                .one();
    }

    public void agree(Long applyId) {
        lambdaUpdate()
                .eq(UserApply::getId, applyId)
                .set(UserApply::getStatus, AGREE.getCode())
                .update();
    }

    public IPage<UserApply> friendApplyPage(Long uid, PageBaseReq request) {
        return lambdaQuery()
                .eq(UserApply::getTargetId, uid)
                .eq(UserApply::getStatus, UNREAD.getCode())
                .orderByDesc(UserApply::getCreateTime)
                .page(request.plusPage());
    }

    public void readApplies(Long uid, List<Long> applyIds) {
        lambdaUpdate()
                .eq(UserApply::getReadStatus, UNREAD.getCode())
                .eq(UserApply::getTargetId, uid)
                .in(UserApply::getId, applyIds)
                .set(UserApply::getReadStatus, READ.getCode())
                .update();
    }

    public Integer getUnRead(Long targetId) {
        return lambdaQuery()
                .eq(UserApply::getTargetId, targetId)
                .eq(UserApply::getReadStatus, UNREAD.getCode())
                .count();
    }
}
